package ru.smc.hogwarts.servlets.students;

import java.io.IOException;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.servlet.RequestDispatcher;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import ru.smc.hogwarts.classes.EMF;
import ru.smc.hogwarts.model.Student;
import ru.smc.hogwarts.model.Assessment;

public class StudentsInfo extends HttpServlet implements Servlet {
	private static final long serialVersionUID = 1L;
	private HttpSession session;
    
    public StudentsInfo() {
        super();
    }
    
	@SuppressWarnings("unchecked")
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		session = request.getSession(true);
    	session.setAttribute("servletName", getServletName());
		
    	long id = 0;
		
		try {
			id = Long.parseLong(request.getParameter("id"));
		} catch (NumberFormatException e) {}
		
		EntityManagerFactory emf = EMF.getInstance().getEMF();
    	EntityManager em = emf.createEntityManager();
		
		Student student = em.find(Student.class, id);
		
		if (student == null) {
			request.getRequestDispatcher("/pages/error/error.jsp").forward(request, response);
		} else {
			request.setAttribute("student", student);
			Query query = em.createQuery("select a from Assessment a where a.student.id = " + id);
	    	int size = query.getResultList().size();
	    	
	    	request.setAttribute("size", size);
			
	    	String action = request.getParameter("page");
			
			if (action == null) {
	    		session.setAttribute("pageBegin", 0);
	    		session.setAttribute("pageEnd", 7);
	    	} else {
	    		int begin = 0;
	    		int end = 7;
	    		
	    		try {
	    			begin = (Integer)session.getAttribute("pageBegin");
	    			end = (Integer)session.getAttribute("pageEnd");
	    		} catch (NullPointerException e) {}
	    		
	    		if (action.equals("next") && end+1 < size) {
	    			begin += 8;
	    			end += 8;
	    		} else if (action.equals("previous") && (begin > 0)) {
	    			begin -= 8;
	    			end -= 8;
	    		}
	    		
	    		session.setAttribute("pageBegin", begin);
	    		session.setAttribute("pageEnd", end);
	    	}
			
			query.setFirstResult((Integer)session.getAttribute("pageBegin"));
	    	query.setMaxResults(8);
	    	List<Assessment> assessments = query.getResultList();
			
			request.setAttribute("assessments", assessments);
			
			Query points = em.createQuery("select sum(p.quantity) from Point p where p.student.id = " + id);
			request.setAttribute("points", points.getSingleResult());
								
			RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/pages/students/studentInfo.jsp");
			dispatcher.forward(request, response);
		}
		
		em.close();
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}

}
